<?php
namespace Admin\Controller;

class SchoolController extends AdminController {
	/**
	 * 学校信息
	 * 
	 * 		获取页面参数查询学校信息。
	 */
	public function index(){
		$schoolname   			=	I('schoolname');
		$map['status']			=	array('in','0,1');//学校状态:-1已删除  0禁用，1正常
		$map['id|schoolname']	=	array('like', '%'.(string)$schoolname.'%');
		$schoolList				=	$this->lists('School', $map);
		$ChinaCityModel			=	D('District');//三级联动查询出各个学校的所在区域
		$schoolList				=	$ChinaCityModel->mergeArray($schoolList);
		$this->assign('_list', $schoolList);
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
		$this->display();
	}
	
	public function save(){
		$schoolModel=	D('School');
		$school 	=	$schoolModel->create();
		if(!$school){
			$this->error($schoolModel->getError());
		}else{
			if(!$school['id']){
				$school['status']=1;
				if(!$schoolModel->add($school)){
					$this->error("添加失败！");
				}else{
					$this->success("添加成功！",Cookie('__forward__'));
				}
			}else {
				if(!$schoolModel->save($school)){
					$this->error("修改失败！");
				}else{
					$this->success("修改成功！",Cookie('__forward__'));
				}
			}
		}
	}
	
	/**
	 * 新建学校
	 */
	public function add(){
		$this->display();
	}
	
	/**
	 * 编辑学校
	 *
	 * 		获取页面参数编辑学校。
	 * 
	 * 		通过学校id查询出学校信息回显数据。
	 */
	public function editSchool(){
		$schoolModel=	M('School');
		$schoolid	=	I('id');
		$school		=	$schoolModel->select($schoolid);			
		$ChinaCity	=	D('District');//三级联动查询出各个学校的所在区域
		$school		= 	$ChinaCity->mergeArray($school);
		$this->assign('_list', $school);
		$this->display();
	}
	
	/**
	 * 修改状态
	 * @param string $method	传入的修改状态命令
	 * 		resumeschool	启用学校
	 * 		forbidschool	禁用学校
	 * 		deleteschool	删除学校
	 */
	public function changeStatus($method=null){
		$map['id'] =   I('id');
		switch ( strtolower($method) ){
			//学校状态:-1已删除  0禁用，1正常
			case 'resumeschool':
				$this->resume('School', $map);
				break;
			case 'forbidschool':
				$this->forbid('School', $map);
				break;
			case 'deleteschool':
				$this->delete('School', $map);
				break;
			default:
				$this->error('参数非法');
		}
	}
}